package com.sham.endpoint;

import javax.jdo.PersistenceManager;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiClass;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.config.Named;
import com.sham.PMF;
import  com.sham.entity.Message;

@Api(name = "myapi", version = "v1",namespace = @ApiNamespace(ownerDomain = "amozesham.appspot.com",ownerName = "amozesham.appspot.com",packagePath=""))
public class MessageAPI {

	@ApiMethod(name="sham.amozesh.message.getmessage", path= "/message/getmessage/" )
	public Message getMessage(@Named("id")Long id){
		
		PersistenceManager pm = getPersistenceManager();
		Message message = new Message();

		try {
			message = pm.getObjectById(Message.class, id);
        } finally {
	            pm.close();
        }
       
		return message;
	}
	
	@ApiMethod(name = "sham.amozesh.message.insert", path="message/insert")
	public Message insertMessage(Message message) {
		PersistenceManager mgr = getPersistenceManager();
		
		try {
			mgr.makePersistent(message);
		} finally {
			mgr.close();
		}
		return message;
	}

	private static PersistenceManager getPersistenceManager() {
		return PMF.get().getPersistenceManager();
	}
	
}
